using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Data;
using Crawler;

namespace CrawlerUserInterface
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private Microsoft.Samples.CurvedPanel curvedPanel1;
		private System.Windows.Forms.Label label1;
		private Microsoft.Samples.RegionBuilder regionBuilder1;
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtDestinationPath;
		private System.Windows.Forms.TextBox txtCachePath;
		private System.Windows.Forms.TextBox txtSeedUrlBox;
		private Microsoft.Samples.BounceButton cmdDownload;
		private Microsoft.Samples.BounceButton cmdAbout;
		private Microsoft.Samples.BounceButton cmdExit;
		private System.Windows.Forms.ListBox listOfDumpedFiles;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label lblCount;
		private System.Windows.Forms.Button exitButton;
		private System.Windows.Forms.Button aboutButton;
		private System.Windows.Forms.Button downloadButton;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label lblFileBeingDownloaded;
		private System.ComponentModel.IContainer components;

		public MainForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			Logger.FileLogged += new FileLoggedHandler(Logger_FileLogged);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
			this.curvedPanel1 = new Microsoft.Samples.CurvedPanel();
			this.lblFileBeingDownloaded = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.exitButton = new System.Windows.Forms.Button();
			this.aboutButton = new System.Windows.Forms.Button();
			this.downloadButton = new System.Windows.Forms.Button();
			this.cmdExit = new Microsoft.Samples.BounceButton();
			this.cmdAbout = new Microsoft.Samples.BounceButton();
			this.cmdDownload = new Microsoft.Samples.BounceButton();
			this.lblCount = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.listOfDumpedFiles = new System.Windows.Forms.ListBox();
			this.label3 = new System.Windows.Forms.Label();
			this.txtDestinationPath = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtCachePath = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.txtSeedUrlBox = new System.Windows.Forms.TextBox();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.regionBuilder1 = new Microsoft.Samples.RegionBuilder(this.components);
			this.curvedPanel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// curvedPanel1
			// 
			this.curvedPanel1.AddToFormRegion = true;
			this.curvedPanel1.Controls.Add(this.lblFileBeingDownloaded);
			this.curvedPanel1.Controls.Add(this.label5);
			this.curvedPanel1.Controls.Add(this.exitButton);
			this.curvedPanel1.Controls.Add(this.aboutButton);
			this.curvedPanel1.Controls.Add(this.downloadButton);
			this.curvedPanel1.Controls.Add(this.cmdExit);
			this.curvedPanel1.Controls.Add(this.cmdAbout);
			this.curvedPanel1.Controls.Add(this.cmdDownload);
			this.curvedPanel1.Controls.Add(this.lblCount);
			this.curvedPanel1.Controls.Add(this.label4);
			this.curvedPanel1.Controls.Add(this.listOfDumpedFiles);
			this.curvedPanel1.Controls.Add(this.label3);
			this.curvedPanel1.Controls.Add(this.txtDestinationPath);
			this.curvedPanel1.Controls.Add(this.label2);
			this.curvedPanel1.Controls.Add(this.txtCachePath);
			this.curvedPanel1.Controls.Add(this.label1);
			this.curvedPanel1.Controls.Add(this.txtSeedUrlBox);
			this.curvedPanel1.Controls.Add(this.pictureBox1);
			this.curvedPanel1.FillColor = System.Drawing.SystemColors.Control;
			this.curvedPanel1.FillTransparency = 0.25;
			this.curvedPanel1.FormBehavior = Microsoft.Samples.FormBehaviors.TitleBar;
			this.curvedPanel1.Location = new System.Drawing.Point(0, 0);
			this.curvedPanel1.LowerLeftCornerRadius = 111;
			this.curvedPanel1.LowerRightCornerRadius = 106;
			this.curvedPanel1.Name = "curvedPanel1";
			this.curvedPanel1.Size = new System.Drawing.Size(344, 440);
			this.curvedPanel1.TabIndex = 0;
			this.curvedPanel1.UpperLeftCornerRadius = 1;
			this.curvedPanel1.UpperRightCornerRadius = 1;
			this.curvedPanel1.Paint += new System.Windows.Forms.PaintEventHandler(this.curvedPanel1_Paint);
			// 
			// lblFileBeingDownloaded
			// 
			this.lblFileBeingDownloaded.Location = new System.Drawing.Point(144, 328);
			this.lblFileBeingDownloaded.Name = "lblFileBeingDownloaded";
			this.lblFileBeingDownloaded.Size = new System.Drawing.Size(184, 16);
			this.lblFileBeingDownloaded.TabIndex = 31;
			this.lblFileBeingDownloaded.Click += new System.EventHandler(this.lblFileBeingDownloaded_Click);
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(32, 328);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(104, 16);
			this.label5.TabIndex = 30;
			this.label5.Text = "File Downloading";
			// 
			// exitButton
			// 
			this.exitButton.Location = new System.Drawing.Point(240, 376);
			this.exitButton.Name = "exitButton";
			this.exitButton.Size = new System.Drawing.Size(80, 40);
			this.exitButton.TabIndex = 29;
			this.exitButton.Text = "Exit";
			this.exitButton.Click += new System.EventHandler(this.exitButton_Click);
			// 
			// aboutButton
			// 
			this.aboutButton.Location = new System.Drawing.Point(160, 376);
			this.aboutButton.Name = "aboutButton";
			this.aboutButton.Size = new System.Drawing.Size(80, 40);
			this.aboutButton.TabIndex = 28;
			this.aboutButton.Text = "About";
			this.aboutButton.Click += new System.EventHandler(this.aboutButton_Click);
			// 
			// downloadButton
			// 
			this.downloadButton.Location = new System.Drawing.Point(24, 376);
			this.downloadButton.Name = "downloadButton";
			this.downloadButton.Size = new System.Drawing.Size(128, 40);
			this.downloadButton.TabIndex = 27;
			this.downloadButton.Text = "Download";
			this.downloadButton.Click += new System.EventHandler(this.button1_Click);
			// 
			// cmdExit
			// 
			this.cmdExit.BackColor = System.Drawing.Color.Transparent;
			this.cmdExit.DialogResult = System.Windows.Forms.DialogResult.None;
			this.cmdExit.Image = ((System.Drawing.Image)(resources.GetObject("cmdExit.Image")));
			this.cmdExit.Location = new System.Drawing.Point(259, 384);
			this.cmdExit.Name = "cmdExit";
			this.cmdExit.Size = new System.Drawing.Size(72, 40);
			this.cmdExit.TabIndex = 21;
			this.cmdExit.Text = "bounceButton1";
			this.cmdExit.Click += new System.EventHandler(this.bounceButton1_Click);
			// 
			// cmdAbout
			// 
			this.cmdAbout.BackColor = System.Drawing.Color.Transparent;
			this.cmdAbout.DialogResult = System.Windows.Forms.DialogResult.None;
			this.cmdAbout.Image = ((System.Drawing.Image)(resources.GetObject("cmdAbout.Image")));
			this.cmdAbout.Location = new System.Drawing.Point(171, 384);
			this.cmdAbout.Name = "cmdAbout";
			this.cmdAbout.Size = new System.Drawing.Size(80, 40);
			this.cmdAbout.TabIndex = 22;
			this.cmdAbout.Text = "bounceButton2";
			this.cmdAbout.Click += new System.EventHandler(this.bounceButton2_Click);
			// 
			// cmdDownload
			// 
			this.cmdDownload.BackColor = System.Drawing.Color.Transparent;
			this.cmdDownload.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("cmdDownload.BackgroundImage")));
			this.cmdDownload.DialogResult = System.Windows.Forms.DialogResult.None;
			this.cmdDownload.Location = new System.Drawing.Point(32, 384);
			this.cmdDownload.Name = "cmdDownload";
			this.cmdDownload.Size = new System.Drawing.Size(128, 32);
			this.cmdDownload.TabIndex = 23;
			this.cmdDownload.Text = "bounceButton3";
			this.cmdDownload.Click += new System.EventHandler(this.bounceButton3_Click);
			// 
			// lblCount
			// 
			this.lblCount.Location = new System.Drawing.Point(147, 352);
			this.lblCount.Name = "lblCount";
			this.lblCount.Size = new System.Drawing.Size(176, 16);
			this.lblCount.TabIndex = 26;
			this.lblCount.Click += new System.EventHandler(this.lblCount_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(27, 352);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(112, 24);
			this.label4.TabIndex = 25;
			this.label4.Text = "DownLoaded  Files :";
			this.label4.Click += new System.EventHandler(this.label4_Click);
			// 
			// listOfDumpedFiles
			// 
			this.listOfDumpedFiles.AllowDrop = true;
			this.listOfDumpedFiles.Location = new System.Drawing.Point(27, 208);
			this.listOfDumpedFiles.Name = "listOfDumpedFiles";
			this.listOfDumpedFiles.ScrollAlwaysVisible = true;
			this.listOfDumpedFiles.Size = new System.Drawing.Size(304, 108);
			this.listOfDumpedFiles.TabIndex = 24;
			this.listOfDumpedFiles.SelectedIndexChanged += new System.EventHandler(this.listOfDumpedFiles_SelectedIndexChanged);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(27, 184);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(104, 16);
			this.label3.TabIndex = 10;
			this.label3.Text = "Destination Folder :";
			this.label3.Click += new System.EventHandler(this.label3_Click);
			// 
			// txtDestinationPath
			// 
			this.txtDestinationPath.Location = new System.Drawing.Point(131, 176);
			this.txtDestinationPath.Name = "txtDestinationPath";
			this.txtDestinationPath.Size = new System.Drawing.Size(200, 20);
			this.txtDestinationPath.TabIndex = 9;
			this.txtDestinationPath.Text = "";
			this.txtDestinationPath.TextChanged += new System.EventHandler(this.txtDestinationPath_TextChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(27, 152);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(104, 16);
			this.label2.TabIndex = 8;
			this.label2.Text = "Local Cache Path :";
			this.label2.Click += new System.EventHandler(this.label2_Click);
			// 
			// txtCachePath
			// 
			this.txtCachePath.Location = new System.Drawing.Point(131, 144);
			this.txtCachePath.Name = "txtCachePath";
			this.txtCachePath.Size = new System.Drawing.Size(200, 20);
			this.txtCachePath.TabIndex = 7;
			this.txtCachePath.Text = "";
			this.txtCachePath.TextChanged += new System.EventHandler(this.txtCachePath_TextChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(19, 120);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(112, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "  URL to Download :";
			this.label1.Click += new System.EventHandler(this.label1_Click);
			// 
			// txtSeedUrlBox
			// 
			this.txtSeedUrlBox.Location = new System.Drawing.Point(131, 112);
			this.txtSeedUrlBox.Name = "txtSeedUrlBox";
			this.txtSeedUrlBox.Size = new System.Drawing.Size(200, 20);
			this.txtSeedUrlBox.TabIndex = 0;
			this.txtSeedUrlBox.Text = "http://";
			this.txtSeedUrlBox.TextChanged += new System.EventHandler(this.txtSeedUrlBox_TextChanged);
			// 
			// pictureBox1
			// 
			this.pictureBox1.BackColor = System.Drawing.SystemColors.Control;
			this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
			this.pictureBox1.Location = new System.Drawing.Point(8, 8);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(320, 72);
			this.pictureBox1.TabIndex = 6;
			this.pictureBox1.TabStop = false;
			this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
			// 
			// regionBuilder1
			// 
			this.regionBuilder1.MakeFormRegion = true;
			this.regionBuilder1.ParentForm = this;
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(376, 454);
			this.Controls.Add(this.curvedPanel1);
			this.Name = "MainForm";
			this.Text = "Crawler";
			this.Load += new System.EventHandler(this.Form1_Load);
			this.curvedPanel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.EnableVisualStyles();
			Application.Run(new MainForm());
		}

		private void Form1_Load(object sender, System.EventArgs e) {
		
		}

		private void curvedPanel1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
		
		}

		private void bounceButton1_Click(object sender, System.EventArgs e) {
			Application.Exit();
		}

		private void bounceButton2_Click(object sender, System.EventArgs e) {
			if(aboutForm == null )
				aboutForm = new About();
			aboutForm.Show() ;
		}

		Form aboutForm = null;

		private const string INVALID_URL_FORMAT_MESSAGE = "The Format Of URL is Invalid !";
		private const string INVALID_CACHE_PATH_FORMAT = "The Format Of Cache Path is Invalid !";
		private const string INVALID_DESTINATION_PATH_FORMAT = "The Format Of Destination Path is Invalid !";


		private void bounceButton3_Click(object sender, System.EventArgs e) {
			if(!ValidateSeedUrl(txtSeedUrlBox.Text))
			{
				MessageBox.Show(INVALID_URL_FORMAT_MESSAGE);
				return;
			}
			string seedUrl = txtSeedUrlBox.Text ;
			if(!ValidateCachePath(txtCachePath.Text))
			{
				MessageBox.Show(INVALID_CACHE_PATH_FORMAT);
				return;
			}
			string cachePath = txtCachePath.Text ;
			if(!ValidateDestinationPath(txtDestinationPath.Text))
			{
				MessageBox.Show(INVALID_DESTINATION_PATH_FORMAT);
				return;
			}
			string destinationPath = txtDestinationPath.Text ;
			string domain = @"http://127.0.0.1/home/";
			Crawler.Crawler crawler = new Crawler.Crawler(domain,cachePath,destinationPath);
			crawler.StartCrawling(seedUrl) ;
			lblCount.Text += "  Done !! ";
		}

		private bool ValidateSeedUrl(string seedUrl)
		{
			bool result = true;
			string pattern = @"^(http|ftp)://((\w+\.)*\w+|(\d{1,3}\.\d{1,3}\.\d{1,3}\d))(:\d+)*/(\w+/)*\w+\.\w{3,3}$";
			Regex regex = new Regex(pattern,RegexOptions.Compiled | RegexOptions.IgnoreCase );	
			result = regex.Match(seedUrl).Success ; 
			return result;
		}

		private bool ValidateCachePath(string cachePath)
		{
			bool result = true;
			result = TryCreatingDirectory(cachePath);
			return result;
		}

		private bool TryCreatingDirectory(string path)
		{
			bool result = true;
			try{
				if(!Directory.Exists(path) )
					Directory.CreateDirectory(path); 
			}catch(Exception e)
			{
				result = false;
			}
			return result;
		}

		private bool ValidateDestinationPath(string destinationPath)
		{
			bool result = true;
			result = TryCreatingDirectory(destinationPath);
			return result;
		}

		private void Logger_FileLogged(string nameOfFileLogged) {
			Application.DoEvents() ;
			this.Refresh() ;
			listOfDumpedFiles.Items.Add(nameOfFileLogged);
			lblFileBeingDownloaded.Text = nameOfFileLogged; 
			lblCount.Text = listOfDumpedFiles.Items.Count.ToString() ;  
		}

		private void listOfDumpedFiles_SelectedIndexChanged(object sender, System.EventArgs e) {
		
		}

		private void label3_Click(object sender, System.EventArgs e) {
		
		}

		private void txtDestinationPath_TextChanged(object sender, System.EventArgs e) {
		
		}

		private void label2_Click(object sender, System.EventArgs e) {
		
		}

		private void txtCachePath_TextChanged(object sender, System.EventArgs e) {
		
		}

		private void label1_Click(object sender, System.EventArgs e) {
		
		}

		private void txtSeedUrlBox_TextChanged(object sender, System.EventArgs e) {
		
		}

		private void pictureBox1_Click(object sender, System.EventArgs e) {
		
		}

		private void lblCount_Click(object sender, System.EventArgs e) {
		
		}

		private void label4_Click(object sender, System.EventArgs e) {
		
		}

		private void button1_Click(object sender, System.EventArgs e) {
			if(!ValidateSeedUrl(txtSeedUrlBox.Text)) {
				MessageBox.Show(INVALID_URL_FORMAT_MESSAGE);
				return;
			}
			string seedUrl = txtSeedUrlBox.Text ;
			if(!ValidateCachePath(txtCachePath.Text)) {
				MessageBox.Show(INVALID_CACHE_PATH_FORMAT);
				return;
			}
			string cachePath = txtCachePath.Text ;
			if(!ValidateDestinationPath(txtDestinationPath.Text )) {
				MessageBox.Show(INVALID_DESTINATION_PATH_FORMAT);
				return;
			}
			string destinationPath = txtDestinationPath.Text ;
			string domain = @"http://127.0.0.1/home/";
			Crawler.Crawler crawler = new Crawler.Crawler(domain,cachePath,destinationPath);
			crawler.StartCrawling(seedUrl) ;
			lblCount.Text += "  Done !! ";
	
		}

		private void exitButton_Click(object sender, System.EventArgs e) {
			Application.Exit();
		}

		private void aboutButton_Click(object sender, System.EventArgs e) {
			if(aboutForm == null )
				aboutForm = new About();
			aboutForm.Show() ;
		}

		private void lblFileBeingDownloaded_Click(object sender, System.EventArgs e) {
		
		}
	}
}
